//
//  NewWordVC.m
//  sweetbox
//
//  Created by apollo on 15/9/8.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "NewWordVC.h"
#import "log.h"
#import "DBHelper.h"

@interface NewWordVC ()

@end

@implementation NewWordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)onCancel:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^(){
        
    }];
}

- (IBAction)onSave:(id)sender {
    NSString *word = self.wordTF.text;
    NSString *translate = self.translateTF.text;
    
    if([word isEqualToString:@""] || [translate isEqualToString:@""]) {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完成" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return ;
    }
    NSLog(@"word :%@",word);
    NSLog(@"translate :%@",translate);
    
    BOOL ret = [[DBHelper sharedDBHelper] insertWord:word translate:translate];
    if (YES == ret) {
        LOG_I("Save word %s Success",[word cStringUsingEncoding:NSUTF8StringEncoding] );
        
    } else {
        LOG_E("Save word %s Failed",[word cStringUsingEncoding:NSUTF8StringEncoding] );
    }
    

    [self dismissViewControllerAnimated:YES completion:^(){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"save_word" object:self];
    }];
}


- (IBAction)onWordTFDidEnd:(id)sender {

    [sender resignFirstResponder];
}

- (IBAction)onTranslateTFDidEnd:(id)sender {
    
    [sender resignFirstResponder];
}

@end
